/*
 * ShoppingCartHelperModel.java
 * business
 *
 * Created by ChenTao on 2016/11/8.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.GoodsAboutRequest;
import com.yidiandao.business.data.request.QueryCartGoodsInfoRequest;
import com.yidiandao.business.data.response.AddToShoppingCartResponse;
import com.yidiandao.business.data.response.BuyNowResponse;
import com.yidiandao.business.data.response.QueryCartGoodsInfoResponse;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 购物车其他常用操作Model
 */
public class ShoppingCartHelperModel implements IShoppingCartHelperModel {

    private NetBiz mNetBiz = new NetBiz();

    @Override
    public void getCatGoodsInfo(String goodsId, String tag, final OnGetGoodsInfoListener listener) {

        QueryCartGoodsInfoRequest request = new QueryCartGoodsInfoRequest();
        request.setUrl(Urls.GET_CART_INFO);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setGoodsId(goodsId);
        request.setTag(tag);

        mNetBiz.sendGetRequest(request, QueryCartGoodsInfoResponse.class,
                new NetListener<QueryCartGoodsInfoResponse>() {
                    @Override
                    public void onSuccess(QueryCartGoodsInfoResponse response) {
                        if (listener != null) {

                            QueryCartGoodsInfoResponse.Data data = response.getData();

                            Goods goods = new Goods();
                            goods.setMinNumber(data.getMinNumber());
                            goods.setLimitNumber(data.getLimitNumber());
                            goods.setGoodsPrice(data.getGoodsPrice());
                            goods.setStockNumber(data.getStockNumber());

                            listener.onSuccess(goods);
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void addToCart(String tag, String goodsId, int count, final OnAddToShoppingCartListener listener) {
        GoodsAboutRequest request = new GoodsAboutRequest();
        request.setTag(tag);
        request.setGoodsId(goodsId);
        request.setUrl(Urls.ADD_TO_CART);
        request.setNumber(count + "");
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        mNetBiz.sendGetRequest(request, AddToShoppingCartResponse.class,
                new NetListener<AddToShoppingCartResponse>() {
                    @Override
                    public void onSuccess(AddToShoppingCartResponse response) {
                        if (listener != null) {
                            AddToShoppingCartResponse.Data data = response.getData();
                            AddToShoppingCartResponse.CartInfo cartInfo = data.getCartInfo();
                            listener.onSuccess(cartInfo.getShop(), cartInfo.getGoods());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void buyNow(String tag, String goodsId, int number, final OnBuyNowListener listener) {

        GoodsAboutRequest request = new GoodsAboutRequest();
        request.setGoodsId(goodsId);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.BUY_NOW);
        request.setNumber(number + "");
        mNetBiz.sendGetRequest(request, BuyNowResponse.class, new NetListener<BuyNowResponse>() {
            @Override
            public void onSuccess(BuyNowResponse response) {
                if (listener != null) {
                    listener.onSuccess();
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                return true;
            }

            @Override
            public void onFinal() {
            }
        });
    }
}
